package fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yztc.thinkpad.mandemo.ContributionActivity;
import com.yztc.thinkpad.mandemo.R;
import com.yztc.thinkpad.mandemo.cooladapter.CoolAdapter;
import com.yztc.thinkpad.mandemo.cooladapter.CoolPagerFragmentAdapter;
import com.yztc.thinkpad.mandemo.coolbean.CoolDataBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2016/9/29.
 */
public class FragmentCool extends Fragment {

    @Bind(R.id.cool_toolbar)
    Toolbar coolToolbar;
    @Bind(R.id.cool_tablayout)
    TabLayout coolTablayout;
    @Bind(R.id.cool_appbar)
    AppBarLayout coolAppbar;
    @Bind(R.id.cool_viewpager)
    ViewPager coolViewpager;
    @Bind(R.id.cool_content)
    CoordinatorLayout coolContent;
    @Bind(R.id.tv_cool_contribution)
    TextView tvCoolContribution;


    //    private ICoolFABToPagerFragment fabcallback;
//
//    //接口回调
//    public void setCallBack(ICoolFABToPagerFragment fabcallback) {
//        this.fabcallback = fabcallback;
//    };

    private Toolbar mToolbar;
    private List<Fragment> fragments;
    private CoolPagerFragmentAdapter fragmentAdapter;
    private View view;
    List<CoolDataBean> coolDataBeen;
    private CoolAdapter adapter;
    private String[] cool_title = {"最热", "搭配", "发型", "护理", "健身", "明星"};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_cool, null);

        ButterKnife.bind(this, view);
        //初始化控件
        initview();
        //数据加载
        initdata();
        initintent();
        return view;
    }
//点击跳转到投稿
    private void initintent() {
        tvCoolContribution.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), ContributionActivity.class);
                getActivity().startActivity(intent);
            }
        });
    }


    private void initview() {
//        coolFab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Toast.makeText(getActivity(),"2222222222",Toast.LENGTH_LONG).show();
//            }
//        });
        //Toolbar即能取代原本的 actionbar 了

        ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
        //设置tablayout的选中状态
        for (int i = 0; i < cool_title.length; i++) {
            if (i == 0) {
                coolTablayout.addTab(coolTablayout.newTab().setText(cool_title[i]), true);
            } else {
                coolTablayout.addTab(coolTablayout.newTab().setText(cool_title[i]), false);
            }
            coolTablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        }

        // 添加viewpager的监听 参数传递tabLayout中提供的监听事件  viewpager中的page页与tab对应
        coolViewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(coolTablayout));
        //设置tabLayout的监听事件  滑动改变tabLayout中的tab时viewpager跟随变化
        coolTablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                coolViewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }

        });
    }


    //初始化适配viewpager的数据
    public void initdata() {
        fragments = new ArrayList<>();
        CoolPagerFragment f1 = new CoolPagerFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("kind", 6);
        f1.setArguments(bundle);

        CoolPagerFragment f2 = new CoolPagerFragment();
        bundle = new Bundle();
        bundle.putInt("kind", 1);
        f2.setArguments(bundle);


        CoolPagerFragment f3 = new CoolPagerFragment();
        bundle = new Bundle();
        bundle.putInt("kind", 2);
        f3.setArguments(bundle);


        CoolPagerFragment f4 = new CoolPagerFragment();
        bundle = new Bundle();
        bundle.putInt("kind", 3);
        f4.setArguments(bundle);


        CoolPagerFragment f5 = new CoolPagerFragment();
        bundle = new Bundle();
        bundle.putInt("kind", 7);
        f5.setArguments(bundle);


        CoolPagerFragment f6 = new CoolPagerFragment();
        bundle = new Bundle();
        bundle.putInt("kind", 5);
        f6.setArguments(bundle);


        fragments.add(f1);
        fragments.add(f2);
        fragments.add(f3);
        fragments.add(f4);
        fragments.add(f5);
        fragments.add(f6);
        fragmentAdapter = new CoolPagerFragmentAdapter(getFragmentManager(), fragments);
        coolViewpager.setAdapter(fragmentAdapter);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
